package action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.components.Push;

import service.DepartmentService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;

import entity.Department;
import entity.PageBean;

public class DepartmentAction extends ActionSupport implements ModelDriven<Department> {

	// 注入
	private DepartmentService departmentService;
	private HttpSession session = ServletActionContext.getRequest().getSession();
	private ValueStack valueStack = ActionContext.getContext().getValueStack();
	// 模型
	private Department department = new Department();
	
	private int currpage = 1;

	public void setCurrpage(int currpage) {
		this.currpage = currpage;
	}

	public int getCurrpage() {
		return currpage;
	}

	// 查询全部
/*
	public String query() {
		
		List<Department> departments = departmentService.queryAll();
		if (departments != null && departments.size() > 0) {
			currpage=1;
			PageBean<Department> pageBean = departmentService .findByPage(currpage);
			valueStack.push(pageBean);
			session.setAttribute("departments", departments);
			return "query_success";
		} else {
			return NONE;
		}
		
	}
*/

	// 分页查询
	public String queryBP() {
		PageBean<Department> pageBean = departmentService.findByPage(currpage);
		ActionContext.getContext().getValueStack().push(pageBean);
		
		return "query_success";
	}

	// 跳转到添加页面
	public String add() {
		return "add_success";
	}
	
	// 保存
	public String save() {
		departmentService.save(department);
		
		return "save_success";
	}
	
	// 跳转到编辑页面,根据id号查询
	public String edit() {
		department = departmentService.findById(department.getDid());
		
		return "edit_success";
	}
	
	// 保存修改
	public String update() {
		departmentService.update(department);
		
		return "modify_success";
	}
	
	// 删除
	public String delete() {
		// 最好先查询再删除，查询对象删除，可以级联删除
		department = departmentService.findById(department.getDid());
		departmentService.delete(department);
		
		return "delete_success";
	}
	
	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Department getModel() {
		// TODO Auto-generated method stub
		return department;
	}

}